package com.gmzg.gmzgoffice;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.UiSettings;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MapActivity extends AppCompatActivity implements AMapLocationListener, LocationSource, AMap.OnCameraChangeListener {


    @BindView(R.id.mapview)
    MapView mMapView;
    //    @BindView(R.id.map)
//    MapView mMapView;
    ///声明AMapLocationClient对象
    private AMapLocationClient mapLocationClient = null;
    //声明AMapLocationClientOption对象，用来设置发起定位的模式和相关参数
    private AMapLocationClientOption mLocationClientOption = null;
    private TextView text;

    private UiSettings mSettings;
    private AMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        ButterKnife.bind(this);
        mMapView.onCreate(savedInstanceState);
        if (map == null) {
            map = mMapView.getMap();
            mSettings = map.getUiSettings();
            mSettings.setZoomControlsEnabled(false);
            mSettings.setMyLocationButtonEnabled(false);
            mSettings.setAllGesturesEnabled(false);
            map.setMyLocationEnabled(true);
            map.setLocationSource(this);

        }
        text = (TextView) findViewById(R.id.text);
    }

    public void location(View view) {
        //初始化定位
        mapLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mapLocationClient.setLocationListener(this);
        mLocationClientOption = new AMapLocationClientOption();
        mLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置单次定位
        mLocationClientOption.setOnceLocation(true);
        //获取最近3s内精度最高的一次定位结果
        mLocationClientOption.setOnceLocationLatest(true);
        //给定位客户端对象设置定位参数
        mapLocationClient.setLocationOption(mLocationClientOption);
        //启动定位
        mapLocationClient.startLocation();


    }

    //回调监听的方法
    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
                //解析定位结果
                amapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                amapLocation.getLatitude();//获取纬度
                amapLocation.getLongitude();//获取经度
                amapLocation.getAccuracy();//获取精度信息
                amapLocation.getAddress();//地址，如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。

                //获取定位时间
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(amapLocation.getTime());
                df.format(date);
                text.setText("地址：" + amapLocation.getPoiName());
                LatLng latLng = new LatLng(amapLocation.getLatitude(), amapLocation.getLongitude());

                CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, 15);
                map.addMarker(new MarkerOptions().
                        position(latLng).
                        title(amapLocation.getAddress()));
                map.animateCamera(update);
                map.setOnCameraChangeListener(this);

            } else {
                Log.d("AmapError", "location Error, ErrCode:"
                        + amapLocation.getErrorCode() + ", errInfo:"
                        + amapLocation.getErrorInfo());
            }

        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，实现地图生命周期管理
        mMapView.onSaveInstanceState(outState);
    }


    //定位监听的回调方法
    @Override
    public void activate(OnLocationChangedListener listener) {
    }

    @Override
    public void deactivate() {

    }

    //可视区域发生改变时
    @Override
    public void onCameraChange(CameraPosition position) {

    }

    @Override
    public void onCameraChangeFinish(CameraPosition position) {

    }
}
